﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PetaPoco;

namespace QuickLifeHall.DAL
{
    public abstract class BaseRepository<T> : IRepository<T> where T : class ,new()
    {
        /// <summary>
        /// 初始化
        /// </summary>
        public Database database
        {
            get { return new Database("QuickLifeHallConnection"); }
        }

        public virtual T Get(object id)
        {
            return database.SingleOrDefault<T>(id);
        }

        public virtual List<T> GetList(Sql sql)
        {
            return database.Fetch<T>(sql).ToList();
        }

        public virtual void Insert(T entity)
        {
            database.Insert(entity);
        }

        public virtual void Update(T entity)
        {
            database.Update(entity);
        }

        public virtual void Delete(object id)
        {
            var entity = Get(id);
            database.Delete(entity);
        }

        public virtual Page<T> Page(long page, long rows, Sql sql)
        {
            return database.Page<T>(page, rows, sql);
        }
    }
}